package ca.quicko.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.appfuse.model.BaseObject;

/**
 * Possible Solution to an Issue
 */
@Entity
public class Solution extends BaseObject implements Comparable, Serializable {
	private static final long serialVersionUID = -6436170937547245514L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	/**
	 * Short description of solution (one-liner)
	 */
	@Column(nullable = false, length = 50)
	private String title;

	@Lob
	private String description;

	/**
	 * Act for which solutions are applicable
	 */
	@ManyToOne(optional = false)
	private Issue issue;

	public Solution() {
		// Entity must have a no-arg public or protected constructor
	}

	public String getDesc() {
		return description;
	}

	public Long getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public Issue getIssue() {
		return issue;
	}

	public void setDesc(String description) {
		this.description = description;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setIssue(Issue issue) {
		this.issue = issue;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		Solution myClass = (Solution) object;
		return new CompareToBuilder().append(this.id, myClass.id).append(
				this.title, myClass.title).append(this.description,
				myClass.description)
		/* .append(this.act, myClass.act) */.toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof Solution)) {
			return false;
		}
		Solution rhs = (Solution) object;
		return new EqualsBuilder().append(this.id, rhs.id).append(this.title,
				rhs.title).append(this.description, rhs.description).append(
				this.issue, rhs.issue).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-1652907861, -550017883).append(this.id)
				.append(this.title).append(this.description).append(this.issue)
				.toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("title", this.title).append(
				"act", this.issue).append("desc", this.getDesc()).append("id",
				this.id).toString();
	}

}
